package com.showboom.elauncher.util;

import android.content.ContentResolver;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.PowerManager;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;

import com.showboom.elauncher.MyAppliacation;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class Util {

    public static final String heart_Url = "https://bps.showboom.cn/api/api/index";
    public static final String exception_url = "https://bps.showboom.cn/api/api/index";
    public static final String base_url = "https://bps.showboom.cn/api/api/index";

    public static String getProperty(String key, String defaultValue) {
        String value = defaultValue;
        try {
            Class<?> c = Class.forName("android.os.SystemProperties");
            Method get = c.getMethod("get", String.class, String.class);
            value = (String) (get.invoke(c, key, "unknown"));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return value;
        }
    }

    public static void setProperty(String key, String value) {
        try {
            Class<?> c = Class.forName("android.os.SystemProperties");
            Method set = c.getMethod("set", String.class, String.class);
            set.invoke(c, key, value);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String getIccid(String clazz, String method, int value, Context context) {
        try {
            TelephonyManager manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            Class<?> c = Class.forName(clazz);
            Method getSimSerialNumber = c.getMethod(method, int.class);
            return (String) getSimSerialNumber.invoke(manager, value);
        } catch (Exception e) {
            e.printStackTrace();
            return "-1";
        }
    }

    public static void screenOn() {
        PowerManager powerManager = (PowerManager) MyAppliacation.context.getSystemService(Context.POWER_SERVICE);
        PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "screen_on");
        wakeLock.acquire();
        wakeLock.release();
    }

    /**
     * @return 返回boolean, 判断网络是否可用, 是否为移动网络
     */

    public static boolean hasWifiConnection(Context context) {
        //获取活动连接管理器
        final ConnectivityManager connectivityManager = (ConnectivityManager) context.
                getSystemService(Context.CONNECTIVITY_SERVICE);
        final NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        return (networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_WIFI);
    }

    public static String getImei(Context context) {
        return ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();
    }

    public static String getIccid() {
        TelephonyManager telephonyManager =  (TelephonyManager) MyAppliacation.context.getSystemService(Context.TELEPHONY_SERVICE);
        return telephonyManager.getSimSerialNumber();
    }

    public static boolean scriptIsRunning() {
        if(readTimeStampFile() == -1l) {
            return false;
        }
        long currentTime = System.currentTimeMillis() / 1000;
        long minusTime = currentTime - readTimeStampFile();
        if(minusTime > 30 * 60) {
            return false;
        } else {
            return true;
        }
    }

    public static long readTimeStampFile() {
        String scriptTimestamp = "";
        try {
            File file = new File("/storage/emulated/0/data/tstime");
            if (file.exists()) {
                FileReader fr = new FileReader(file);
                BufferedReader br = new BufferedReader(fr);
                scriptTimestamp = br.readLine();
                Log.d("leihuiming", "scriptTimestamp=" + scriptTimestamp);
                return Long.parseLong(scriptTimestamp.trim());
            }
        } catch (IOException e) {
            e.printStackTrace();
            return -1l;
        }
        return -1l;
    }

    public static String getPackageVersion(String pkgName) {
        PackageManager manager = MyAppliacation.context.getPackageManager();
        try {
            PackageInfo info = manager.getPackageInfo(pkgName, 0);
            //Log.d("leihuiming", "get pkg version= " + info.versionName);
            return info.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return "";
        }
    }

}
